package com.liuqi.jobtask;

import cn.hutool.core.date.DateUtil;
import com.liuqi.business.model.MiningOrderModel;
import com.liuqi.business.model.MiningOrderModelDto;
import com.liuqi.business.service.MiningConfigService;
import com.liuqi.business.service.MiningOrderService;
import com.liuqi.redis.RedisRepository;
import com.liuqi.utils.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: Mr.Liu
 * @date: 2021/3/19 18:42
 * @version: 1.0
 * @class: ContractOrderJob
 * @desc: 定期更新合约订单到期天数
 */
@Slf4j
public class ContractOrderJob implements Job {

    @Autowired
    private RedisRepository redisRepository;

    @Autowired
    private MiningOrderService miningOrderService;

    @Autowired
    private MiningConfigService miningConfigService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        log.info("ContractOrderJob定时任务开始执行");
        String key = "contractOrder:" + DateTimeUtils.currentDate("yyyy-MM-dd");
        if (!redisRepository.hasKey(key)) {
            log.info("ContractOrderJob定时任务执行");
            executeContractOrderJob();
            redisRepository.set(key, "contract", 10, TimeUnit.HOURS);
        } else {
            log.info("ContractJob定时任务已经执行过，不在执行");
        }
        log.info("ContractJob定时任务执行结束");
    }

    /**
     * 执行具体逻辑
     */
    private void executeContractOrderJob() {
        MiningOrderModelDto orderModelDto = new MiningOrderModelDto();
        orderModelDto.setPayStatus(1);
        orderModelDto.setIsOut(0);
        List<MiningOrderModelDto> orderModelDtos = miningOrderService.queryListByDto(orderModelDto, true);
        if (CollectionUtils.isNotEmpty(orderModelDtos)) {
            orderModelDtos.forEach(orderModel->{
                String dayStr = null;
                String expireStr = null;
                try {
                    dayStr = TimeDiff(DateUtil.format(orderModel.getCreateTime(), "yyyy-MM-dd HH:mm"), DateUtil.format(orderModel.getExpireTime(), "yyyy-MM-dd HH:mm"));
                    expireStr = TimeDiff(DateUtil.format(orderModel.getExpireTime(), "yyyy-MM-dd HH:mm"), DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
                } catch (Exception e) {
                    log.info("日期解析异常");
                }

                MiningOrderModel model = new MiningOrderModel();
                //判断过期时间是否大于当前时间
                if(Integer.valueOf(expireStr)<0){
                    model.setExpireDay(0);
                }else {
                    // 周期为1天
                    model.setId(orderModel.getId());
                    model.setExpireDay(Integer.valueOf(dayStr));
                }
                miningOrderService.update(model);
            });
        }
    }



    /**
     * 计算时间段天数
     * @param pBeginTime 开始时间
     * @param pEndTime 结束时间
     * @return
     * @throws Exception
     */
    public static String TimeDiff(String pBeginTime, String pEndTime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long beginL = format.parse(pBeginTime).getTime();
        Long endL = format.parse(pEndTime).getTime();
        Long day = (endL - beginL)/86400000;
        Long hour = ((endL - beginL)%86400000)/3600000;
        Long min = ((endL - beginL)%86400000%3600000)/60000;
        return String.valueOf(day);
    }
}
